using System;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

using Microsoft.SharePoint;
using SPWebParts = Microsoft.SharePoint.WebPartPages;

using Paradoxlost.WebParts.Calendar;
using Paradoxlost.WebParts.Microsoft.Exchange;

namespace Paradoxlost.WebParts.WebService
{
	[WebService(Namespace = "http://www.paradoxlost.com/projects/spexcal")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[ScriptService]
	public class Calendar : System.Web.Services.WebService
	{
		public Calendar()
		{
		}

		private ExchangeServiceBinding GetExchangeService(out string mailbox)
		{
			mailbox = null;

			SPWebParts.SPLimitedWebPartManager manager = SPContext.Current.Web.GetLimitedWebPartManager(
				HttpContext.Current.Request.UrlReferrer.ToString(),
				PersonalizationScope.Shared);

			ExchangeCalendarWebPart exchangePart = null;
			foreach (WebPart wp in manager.WebParts)
			{
				exchangePart = wp as ExchangeCalendarWebPart;
				if (exchangePart != null)
					break;
				wp.Dispose();
			}

			ExchangeServiceBinding ews = new ExchangeServiceBinding();
			ews.Url = exchangePart.EwsAddress;
			ews.Credentials = new System.Net.NetworkCredential(exchangePart.EwsUsername, exchangePart.EwsPassword);
			//http://blogs.msdn.com/webdav_101/archive/2009/01/05/sample-using-calendar-views-with-ews.aspx
			//http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

			if (!string.IsNullOrEmpty(exchangePart.EwsMailBox))
			{
				mailbox = exchangePart.EwsMailBox;
			}

			exchangePart.Dispose();
			manager.Web.Dispose();
			manager.Dispose();

			return ews;
		}

		[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
		[WebMethod]
		public string GetCalendarItemBody(string itemId)
		{
			string result = null;

			string mailbox = null;
			ExchangeServiceBinding ews = GetExchangeService(out mailbox);

			GetItemType request = new GetItemType()
			{
				ItemIds = new BaseItemIdType[]
				{
					new ItemIdType() { Id = itemId }
				},
				ItemShape = new ItemResponseShapeType()
				{
					BaseShape = DefaultShapeNamesType.AllProperties,
					BodyType = BodyTypeResponseType.Text,
					BodyTypeSpecified = true
				}
			};

			GetItemResponseType response = ews.GetItem(request);
			ItemInfoResponseMessageType message = (ItemInfoResponseMessageType)response.ResponseMessages.Items[0];

			if (message.ResponseClass == ResponseClassType.Success)
			{
				ItemType item = message.Items.Items[0];
				result = item.Body.Value;
			}

			return result;
		}

		[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
		[WebMethod]
		public CalendarItem[] GetCalendarItems(DateTime start, DateTime end)
		{
			string mailbox = null;
			ExchangeServiceBinding ews = GetExchangeService(out mailbox);

			CalendarView view = new CalendarView(ews, start, end);
			view.MailBox = mailbox;

			if (view != null)
				return view.GetItems();

			return null;
		}

	}

}